import models, datetime

def confirm_payment(sender, **kwargs):
    ipn_obj = sender
    # Undertake some action depending upon `ipn_obj`.
    reference_key = ipn_obj.custom
    try:
        sr = models.ServiceRequest.objects.get_by_reference_key(reference_key)
        if sr:
            sr.payment_status = ipn_obj.payment_status
            sr.invoice_number = ipn_obj.invoice
            sr.amount_paid = ipn_obj.mc_gross
            sr.payment_transaction_id = ipn_obj.txn_id
            sr.payment_method = ipn_obj.txn_type
            sr.date_paid = datetime.datetime.now()
            sr.is_paid = True
            sr.status = models.REQUEST_STATUS_ORIGINATED
            sr.save()
    except:
        print "Error: confirm_payment"
        pass
